package com.wl.bbs.dao;

import com.wl.bbs.entity.Board;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * BBS论坛-板块表-13 Mapper 接口
 * </p>
 *
 * @author ly
 * @since 2024-11-23
 */
public interface BoardMapper extends BaseMapper<Board> {

    @Select("select a.*, topic_cnt, " +
            "       c.publishtime `maxTopic.publishtime`," +
            "       c.title `maxTopic.title`," +
            "       c.cname `maxTopic.user.cname`" +
            "  from tbl_board a" +
            "  left join (select boardid,count(*) topic_cnt" +
            "               from tbl_topic" +
            "              group by boardid) b on a.id=b.boardid" +
            "  left join (select a.publishtime,a.title,b.cname,a.boardid" +
            "               from tbl_topic a" +
            "               join tbl_user b on a.uid=b.id" +
            "              where a.id in (select max(id) maxid" +
            "                               from tbl_topic" +
            "                              group by boardid)" +
            "            ) c on a.id=c.boardid"+
            " where a.parentid=#{pid}")
    // @Results => <ResultMap>
    @Results(
            id = "rm1",
            value = {@Result(column = "id", property = "id", id = true),
                    @Result(column = "parentid", property = "parentid"),
                    @Result(column = "id", property = "children",
                            many = @Many(select = "selectByPid"))}
    )
    public List<Board> selectByPid(int pid);

}